Skip to content

SliceColorCategoriesProxyModel

Base classes

Name Children Inherits
SliceIdentityProxyModel
prettyqt.itemmodels.proxies.sliceidentityproxymodel
Base model for slice proxy models.

â‹” Inheritance diagram

graph TD
  2809350507["itemmodels.SliceColorCategoriesProxyModel"]
  4214724289["itemmodels.SliceIdentityProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  4214724289 --> 2809350507
  527136029 --> 4214724289
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: SliceIdentityProxyModel

Proxy model to apply coloring to categories.

Example

model = MyModel()
table = widgets.TableView()
table.set_model(model)
table[:, :3].proxify.color_categories()
table.show()
# or
indexer = (slice(None), slice(None, 3))
proxy = itemmodels.SliceColorCategoriesProxyModel(indexer=indexer)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt/itemmodels/proxies/slicecolorcategoriesproxymodel.py
class SliceColorCategoriesProxyModel(itemmodels.SliceIdentityProxyModel):
    """Proxy model to apply coloring to categories.

    ### Example

    ```py
    model = MyModel()
    table = widgets.TableView()
    table.set_model(model)
    table[:, :3].proxify.color_categories()
    table.show()
    # or
    indexer = (slice(None), slice(None, 3))
    proxy = itemmodels.SliceColorCategoriesProxyModel(indexer=indexer)
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "color_categories"
    ICON = "mdi.palette-outline"

    def __init__(self, *args, **kwargs):
        self._role = constants.DISPLAY_ROLE
        self._color_map = {}
        self.color_generator = itertools.cycle(gui.Palette().iter_colors())
        self._color_none = False
        self._cast_to_str = False
        super().__init__(*args, **kwargs)

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not self.indexer_contains(index):
            return super().data(index, role)
        match role:
            case constants.BACKGROUND_ROLE:
                return self.get_color_for_index(index)
            case _:
                return super().data(index, role)

    def get_color_for_index(self, index):
        value = super().data(index, self._role)
        key = str(value) if self._cast_to_str else value
        match value:
            case None if not self._color_none:
                return None
            case _:
                if key in self._color_map:
                    return self._color_map[key]
                new = next(self.color_generator)
                self._color_map[key] = new
                return self._color_map[key]

    def is_none_colored(self) -> bool:
        return self._color_none

    def set_none_colored(self, val: bool):
        with self.change_layout():
            self._color_none = val

    def is_casted_to_str(self) -> bool:
        return self._cast_to_str

    def set_cast_to_str(self, val: bool):
        with self.change_layout():
            self._color_map = {}
            self.color_generator = itertools.cycle(gui.Palette().iter_colors())
            self._cast_to_str = val

    color_none = core.Property(
        bool,
        is_none_colored,
        set_none_colored,
        doc="Whether None-values should also get colored",
    )
    """Color ItemData with value `None`."""

    cast_to_str = core.Property(
        bool,
        is_casted_to_str,
        set_cast_to_str,
        doc="Cast values to string in order to color / group them",
    )
    """Cast all values to a string for deciding whether cells are in same category."""

cast_to_str = core.Property(bool, is_casted_to_str, set_cast_to_str, doc='Cast values to string in order to color / group them') class-attribute instance-attribute

Cast all values to a string for deciding whether cells are in same category.

color_none = core.Property(bool, is_none_colored, set_none_colored, doc='Whether None-values should also get colored') class-attribute instance-attribute

Color ItemData with value None.

:fa-brands-github: Show source on GitHub